﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.models {
    import flash.events.*;
    import flash.display.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import org.robotlegs.mvcs.*;

    public class KeyboardInput extends Actor {

        private const CHEATCODE:String = "P+E+T";

        public var environmentModel:EnvironmentModel;
        private var m_ascii:Array;
        private var m_keyBuffer:Array;
        private var m_timeLastKeyPressed:int;// = 0
        private var _cheatingEnabled:Boolean;
        private var _keyboardEnabled:Boolean;// = true

        public function KeyboardInput(){
            this.m_ascii = [];
            this.m_keyBuffer = [];
            super();
        }
        public function setStage(_arg1:Stage):void{
            this.fillAscii();
            _arg1.addEventListener(KeyboardEvent.KEY_DOWN, this.keyPress);
            _arg1.addEventListener(KeyboardEvent.KEY_UP, this.keyRelease);
        }
        public function getKeyString(_arg1:int):String{
            return (this.m_ascii[_arg1]);
        }
        private function fillAscii():void{
            this.m_ascii[65] = "A";
            this.m_ascii[66] = "B";
            this.m_ascii[67] = "C";
            this.m_ascii[68] = "D";
            this.m_ascii[69] = "E";
            this.m_ascii[70] = "F";
            this.m_ascii[71] = "G";
            this.m_ascii[72] = "H";
            this.m_ascii[73] = "I";
            this.m_ascii[74] = "J";
            this.m_ascii[75] = "K";
            this.m_ascii[76] = "L";
            this.m_ascii[77] = "M";
            this.m_ascii[78] = "N";
            this.m_ascii[79] = "O";
            this.m_ascii[80] = "P";
            this.m_ascii[81] = "Q";
            this.m_ascii[82] = "R";
            this.m_ascii[83] = "S";
            this.m_ascii[84] = "T";
            this.m_ascii[85] = "U";
            this.m_ascii[86] = "V";
            this.m_ascii[87] = "W";
            this.m_ascii[88] = "X";
            this.m_ascii[89] = "Y";
            this.m_ascii[90] = "Z";
            this.m_ascii[48] = "0";
            this.m_ascii[49] = "1";
            this.m_ascii[50] = "2";
            this.m_ascii[51] = "3";
            this.m_ascii[52] = "4";
            this.m_ascii[53] = "5";
            this.m_ascii[54] = "6";
            this.m_ascii[55] = "7";
            this.m_ascii[56] = "8";
            this.m_ascii[57] = "9";
            this.m_ascii[32] = "Space";
            this.m_ascii[13] = "Enter";
            this.m_ascii[17] = "Ctrl";
            this.m_ascii[16] = "Shift";
            this.m_ascii[192] = "~";
            this.m_ascii[38] = "Up";
            this.m_ascii[40] = "Down";
            this.m_ascii[37] = "Left";
            this.m_ascii[39] = "Right";
            this.m_ascii[96] = "Numpad 0";
            this.m_ascii[97] = "Numpad 1";
            this.m_ascii[98] = "Numpad 2";
            this.m_ascii[99] = "Numpad 3";
            this.m_ascii[100] = "Numpad 4";
            this.m_ascii[101] = "Numpad 5";
            this.m_ascii[102] = "Numpad 6";
            this.m_ascii[103] = "Numpad 7";
            this.m_ascii[104] = "Numpad 8";
            this.m_ascii[105] = "Numpad 9";
            this.m_ascii[111] = "Numpad /";
            this.m_ascii[106] = "Numpad *";
            this.m_ascii[109] = "Numpad -";
            this.m_ascii[107] = "Numpad +";
            this.m_ascii[110] = "Numpad .";
            this.m_ascii[45] = "Insert";
            this.m_ascii[46] = "Delete";
            this.m_ascii[33] = "Page Up";
            this.m_ascii[34] = "Page Down";
            this.m_ascii[35] = "End";
            this.m_ascii[36] = "Home";
            this.m_ascii[112] = "F1";
            this.m_ascii[113] = "F2";
            this.m_ascii[114] = "F3";
            this.m_ascii[115] = "F4";
            this.m_ascii[116] = "F5";
            this.m_ascii[117] = "F6";
            this.m_ascii[118] = "F7";
            this.m_ascii[119] = "F8";
            this.m_ascii[188] = ",";
            this.m_ascii[190] = ".";
            this.m_ascii[186] = ";";
            this.m_ascii[222] = "'";
            this.m_ascii[219] = "[";
            this.m_ascii[221] = "]";
            this.m_ascii[189] = "-";
            this.m_ascii[187] = "+";
            this.m_ascii[220] = "\\";
            this.m_ascii[191] = "/";
            this.m_ascii[9] = "TAB";
            this.m_ascii[8] = "Backspace";
            this.m_ascii[27] = "ESC";
        }
        private function keyPress(_arg1:KeyboardEvent):void{
            if ((getTimer() - this.m_timeLastKeyPressed) > 1500){
                this.m_keyBuffer = [];
            };
            var _local2:uint = _arg1.keyCode;
            this.m_keyBuffer.push(_local2);
            this.m_timeLastKeyPressed = getTimer();
            this.checkForCheatCode();
            ((this._keyboardEnabled) && (dispatch(new KeyboardInputEvent(KeyboardInputEvent.ON_KEYS_PRESSED, this.m_keyBuffer.concat()))));
        }
        private function checkForCheatCode():void{
            if (this.areAllKeysInArray(this.CHEATCODE.split("+"))){
                ((this._cheatingEnabled) && (dispatch(new KeyboardInputEvent(KeyboardInputEvent.ON_CHEAT_MODE))));
            };
        }
        private function areAllKeysInArray(_arg1:Array):Boolean{
            var _local3:String;
            var _local5:int;
            if (this.m_keyBuffer.length != _arg1.length){
                return (false);
            };
            var _local2:int;
            var _local4:int = this.m_keyBuffer.length;
            while (_local4--) {
                _local3 = this.getKeyString(this.m_keyBuffer[_local4]);
                _local5 = _arg1.length;
                while (_local5--) {
                    if (_arg1[_local5] == _local3){
                        _local2++;
                    };
                    if (_local2 == _arg1.length){
                        return (true);
                    };
                };
            };
            return (false);
        }
        private function keyRelease(_arg1:KeyboardEvent):void{
        }
        public function get cheatingEnabled():Boolean{
            return (this._cheatingEnabled);
        }
        public function set cheatingEnabled(_arg1:Boolean):void{
            this._cheatingEnabled = _arg1;
        }
        public function get keyboardEnabled():Boolean{
            return (this._keyboardEnabled);
        }
        public function set keyboardEnabled(_arg1:Boolean):void{
            this._keyboardEnabled = _arg1;
        }

    }
}//package net.wooga.pets.models 
